package quq.missq.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import quq.missq.Constants;
import quq.missq.R;
import quq.missq.activity.BigShootDetailActivity;
import quq.missq.beans.BigShootBean;
import quq.missq.beans.MyCollectionBean;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 7/27/15.
 */
public class CollectionBigShootAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int TYPE_FOOTER = 0;
    private static final int TYPE_SUBSCRIBLE = 1;

    private Context mContext;
    private LinkedList<MyCollectionBean.Result> results;
    private LayoutInflater mInflater;


    public CollectionBigShootAdapter(Context context){
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(LinkedList<MyCollectionBean.Result> results){
        this.results = results;
    }

    public LinkedList<MyCollectionBean.Result> getData(){
        return this.results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if(viewType == TYPE_FOOTER){
            View view = mInflater.inflate(R.layout.foot_layout,null);
            return new FootViewHolder(view);
        }else if(viewType == TYPE_SUBSCRIBLE){
            View view = mInflater.inflate(R.layout.collection_big_shoot_adapter,null);
            return new BigshootHolder(view);
        }else{
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder instanceof BigshootHolder){
            BigshootHolder holder = (BigshootHolder)viewHolder;
            final MyCollectionBean.Result result = results.get(position);
            int categoryId = result.getPost().getCategory().getId();
            int imgId = -1;
            if(categoryId == 2){
                imgId = R.mipmap.icon_change;
            }else if(categoryId == 3){
                imgId = R.mipmap.icon_plan;
            }else{
                imgId = R.mipmap.icon_night;
            }
            holder.typeIcon.setImageResource(imgId);
            holder.title.setText(result.getPost().getTitle());
            if(result.getPost().getAuthor().getGender() == 1){
                holder.gender.setText("男");
            }else{
                holder.gender.setText("女");
            }
            holder.level.setText("level "+result.getPost().getAuthor().getLevel());
            //holder.contentImage.setImageUrl(Constants.IMAGE_HOST+result.getPost().getAttachs().get(0).getThumbnail(),VolleyTool.getImageLoader());
            ImageLoadUtil.loadImage(holder.contentImage,Constants.IMAGE_HOST+result.getPost().getAttachs().get(0).getThumbnail(),R.mipmap.empty_photo);
            holder.avtar.setImageUrl(Constants.IMAGE_HOST+result.getPost().getAuthor().getAvatar(),VolleyTool.getImageLoader());
            holder.name.setText(result.getPost().getAuthor().getNkname());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, BigShootDetailActivity.class);
                    intent.putExtra("id",result.getPost().getId());
                    mContext.startActivity(intent);
                }
            });
            holder.quxiao.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle("提醒").setMessage("删除收藏")
                            .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    results.remove(results.get(position));
                                    CollectionBigShootAdapter.this.notifyDataSetChanged();
                                    Map<String,String> params = new HashMap<>();
                                    params.put("id",result.getPost().getId()+"");
                                    params.put("acc_token", UserTools.getUser(mContext).getAcc_token());
                                    VolleyTool.get((Activity)mContext,Constants.UNFAVOR_BIG_SHOOT_URL,params,
                                            null,0,null);
                                }
                            }).show();
                }
            });
        }else if(viewHolder instanceof FootViewHolder){
            FootViewHolder footHolder = (FootViewHolder)viewHolder;
            footHolder.itemView.setTag("footView");
            footHolder.itemView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemViewType(int position) {
        //return super.getItemViewType(position);
        if(position+1 == getItemCount()){
            return TYPE_FOOTER;
        }else{
            return TYPE_SUBSCRIBLE;
           // return results.get(position).getType();
        }
    }

    @Override
    public int getItemCount() {
        return results.size()+1;
    }

    class BigshootHolder extends RecyclerView.ViewHolder{

        ImageView typeIcon;
        TextView title;
        ImageView contentImage;
        NetworkImageView avtar;
        TextView name;
        TextView gender;
        TextView level;
        ImageView quxiao;

        public BigshootHolder(View itemView) {
            super(itemView);
            typeIcon = (ImageView)itemView.findViewById(R.id.typeIcon);
            title = (TextView)itemView.findViewById(R.id.title);
            contentImage = (ImageView)itemView.findViewById(R.id.contentImage);
            avtar = (NetworkImageView)itemView.findViewById(R.id.avtar);
            name = (TextView)itemView.findViewById(R.id.name);
            gender = (TextView)itemView.findViewById(R.id.gender);
            level = (TextView)itemView.findViewById(R.id.level);
            quxiao = (ImageView)itemView.findViewById(R.id.quxiao);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder{

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
